﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;
using System.Data;
using SQLExecute;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;




public partial class reports_report : System.Web.UI.Page
{
    DataSet ds;
    ReportDocument rptDoc = new ReportDocument();
    ParameterField paramField = new ParameterField();
    ParameterFields paramFields = new ParameterFields();
    ParameterDiscreteValue paramDiscretevalues = new ParameterDiscreteValue();
    ParameterValues paramValues = new ParameterValues();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] == null && Session["fullname"] == null)
            Response.Redirect("../login/login.aspx");
        else
            lUsername.Text = " " + Session["username"].ToString();

    }


   
    protected void btnView_Click(object sender, EventArgs e)
    {
        string s1, s2;

        if (txtFrom.Text != "" && txtTo.Text != "")
        {
            s1 = Common.Convert_DDMMYYYY_To_MMDDYYY(txtFrom.Text);
            s2 = Common.Convert_DDMMYYYY_To_MMDDYYY(txtTo.Text);

            ds = SqlHelper.ExecuteDataset(Common.strConnect, "Report_Known_From", new object[] { s1, s2 });

            if (ds.Tables[0].Rows.Count > 0)
            {
                chartKnownFrom.Series["Series1"].Points.AddXY("Friends", Convert.ToDouble(ds.Tables[0].Rows[0]["Friends"].ToString()));
                chartKnownFrom.Series["Series1"].Points.AddXY("Ads on Internet", Convert.ToDouble(ds.Tables[0].Rows[0]["Ads_on_Internet"].ToString()));
                chartKnownFrom.Series["Series1"].Points.AddXY("Ads onnewspaper", Convert.ToDouble(ds.Tables[0].Rows[0]["Ads_on_newspaper"].ToString()));
                chartKnownFrom.Series["Series1"].Points.AddXY("Our websites", Convert.ToDouble(ds.Tables[0].Rows[0]["Our_websites"].ToString()));
                chartKnownFrom.Series["Series1"].Points.AddXY("Vietnamworks", Convert.ToDouble(ds.Tables[0].Rows[0]["Vietnamworks"].ToString()));

                int nFriends, nAdsInternet, nAdsnewspaper, nWebsites, nVietnamworks;
                nFriends = Convert.ToInt16(ds.Tables[0].Rows[0]["Friends"].ToString());
                nAdsInternet = Convert.ToInt16(ds.Tables[0].Rows[0]["Ads_on_Internet"].ToString());
                nAdsnewspaper = Convert.ToInt16(ds.Tables[0].Rows[0]["Ads_on_newspaper"].ToString());
                nWebsites = Convert.ToInt16(ds.Tables[0].Rows[0]["Our_websites"].ToString());
                nVietnamworks = Convert.ToInt16(ds.Tables[0].Rows[0]["Vietnamworks"].ToString());


                int total = nFriends + nAdsInternet + nAdsnewspaper + nWebsites + nVietnamworks;


                ltTotalItems.Text = total.ToString();
                lNumberFriends.Text = ds.Tables[0].Rows[0]["Friends"].ToString() + " (tỷ lệ " + (Decimal.Divide(nFriends, total) * 100).ToString("0.0") + "%)";
                lAdsOnInternet.Text = ds.Tables[0].Rows[0]["Ads_on_Internet"].ToString() + " (tỷ lệ " + (Decimal.Divide(nAdsInternet, total) * 100).ToString("0.0") + "%)";
                lNewspaper.Text = ds.Tables[0].Rows[0]["Ads_on_newspaper"].ToString() + " (tỷ lệ " + (Decimal.Divide(nAdsnewspaper, total) * 100).ToString("0.0") + "%)";
                lOurwebsites.Text = ds.Tables[0].Rows[0]["Our_websites"].ToString() + " (tỷ lệ " + (Decimal.Divide(nWebsites, total) * 100).ToString("0.0") + "%)";
                lVietnamworks.Text = ds.Tables[0].Rows[0]["Vietnamworks"].ToString() + " (tỷ lệ " + (Decimal.Divide(nVietnamworks, total) * 100).ToString("0.0") + "%)";
            }
        }
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {

        paramField.Name = "@from";
        string s1 = Common.Convert_DDMMYYYY_To_MMDDYYY(txtFrom.Text);
        paramDiscretevalues.Value = s1;
        paramField.CurrentValues.Add(paramDiscretevalues);
        paramFields.Add(paramField);

        
        paramField = new ParameterField();
        paramField.Name = "@to";
        string s2 = Common.Convert_DDMMYYYY_To_MMDDYYY(txtTo.Text);
        paramDiscretevalues = new ParameterDiscreteValue();
        paramDiscretevalues.Value = s2;
        paramField.CurrentValues.Add(paramDiscretevalues);
        paramFields.Add(paramField);        
        rptView.ParameterFieldInfo = paramFields;
        
        rptDoc.Load(Server.MapPath("~/reports/rpt_knownfrom2.rpt"));
        Common.SetConnectionInfo(rptDoc.Database.Tables);
        //rptView.ReportSource = rptDoc;
        
        //rptView.Visible = true;
        rptDoc.SetParameterValue("@from", s1);
        rptDoc.SetParameterValue("@to", s2);

        rptDoc.ExportToDisk(ExportFormatType.PortableDocFormat, Server.MapPath("~/export/knownfrom2.pdf"));
        ClientScript.RegisterStartupScript(this.Page.GetType(), "popupOpener", "var popup=window.open('../export/knownfrom2.pdf');popup.focus();", true);
       
    }
    protected void lnkLogout_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
       
        Session.Clear();
        Response.Redirect("../login/login.aspx");
    }
}
